﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Logic.Models;
using Portal.Logic.Providers;
using Portal.Model;

namespace Portal.Logic.Creators
{
	/// <summary>
	/// Class <see cref="UserCreator"/> define user creator
	/// </summary>
	public class UserCreator : IUserCreator
	{

		#region Methods
		/// <summary>
		/// Create user based on logon
		/// </summary>
		/// <param name="logon"></param>
		/// <returns></returns>
		public IUser From(string logon)
		{

			if (string.IsNullOrWhiteSpace(logon))
			{
				return null;
			}

			var user = new User();
			user.Login = logon;
			SetName(user, logon);
			return user;
		}

		private void SetName(User user, string logon)
		{
			string[] logonSplit = logon.Split('\\');
			if (logonSplit.Length == 2)
			{
				string[] nameSplit = logonSplit[1].Split('.');
				if (nameSplit.Length == 2)
				{
					user.FirstName = nameSplit[0].Trim().FirstCharToUpper();
					user.LastName = nameSplit[1].Trim().FirstCharToUpper();
				}
			}
		}

		#endregion
	}
}
